package com.piggy.settlement.sdk.client.v2.basics;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.v2.PiggyOpenBasicsConstants;
import com.piggy.settlement.sdk.util.v2.PiggyOpenBasicsVerifyUtil;

/**
 * @author ：daxia
 * @description：开放平台基础能力
 * @modified By：
 * @version: 1.0$
 */
public class PiggyOpenBasicsClient {

    static PiggyOpenBasicsClient piggyOpenBasicsClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggyOpenBasicsClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggyOpenBasicsClient getInstanse(String appSecret, String domain) {
        if (piggyOpenBasicsClient == null) {
            piggyOpenBasicsClient =
                    new PiggyOpenBasicsClient(appSecret, domain);
        }

        return piggyOpenBasicsClient;
    }

    /**
     * 税源地信息查询
     *
     * @param basics 参数实体：
     * @return
     */
    public JSONObject getTaxFundList(Object basics) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, basics);

            JSONObject paramObj = JSONUtil.parseObj(basics);
            paramObj.putOpt("sign", signStr);
            String response = HttpRequest.post(domain + PiggyOpenBasicsConstants.GET_TAX_FUND_LIST)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-查询税源地信息失败:");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询企业可用资金
     *
     * @param basics 参数实体：
     * @return
     */
    public JSONObject getCashPooling(Object basics) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, basics);

            JSONObject paramObj = JSONUtil.parseObj(basics);
            paramObj.putOpt("sign", signStr);
            String response = HttpRequest.post(domain + PiggyOpenBasicsConstants.GET_CASH_POOLING)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-查询企业可用资金失败:");
            e.printStackTrace();
            return null;
        }

    }

}
